package paziente;

import classi.utente;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Visualizza tutte le vaccinazioni di un paziente
 *
 * @author sestari
 */
public class listaVaccinazione extends HttpServlet {

    /**
     *  Crea la pagina HTML
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {

                ServletContext sc = getServletContext();
                Connection con = (Connection) sc.getAttribute("con");


                HttpSession session = request.getSession();
                utente user = (utente) session.getAttribute("user");

                String id;
                //se id è nullo, significa che la servlet è chiamata da un paziente,allora id è preso della sessione
                //altrimenti è preso dal parametro e significa che un medico vuole vedere le vacinazzioni di un dato paziente
                if (request.getParameter("idpaziente") == null) {
                    id = String.valueOf(user.getId());
                } else {
                    id = request.getParameter("idpaziente");
                }

                //Visualizza tutte le vaccinazioni di un paziente
                String comand = "SELECT v.data_vaccinazione, p.nome, p.cognome FROM  vaccinazioni as v , Persone as p where p.id =v.id_medico and v.id_paziente=\"" + id + "\" order by v.data_vaccinazione DESC";


                Statement st = (Statement) con.createStatement();
                ResultSet rs = (ResultSet) st.executeQuery(comand);



                out.println("<!DOCTYPE html>  ");
                out.println("<html> ");
                out.println("    <head> ");
                out.println("        <title>Lista delle vaccinazioni</title> ");
                out.println("    </head> ");
                out.println("    <body> ");
                out.println("<table border=\"1\"");
                out.println("<tr bgcolor=\"#DEDFF7\">");


                out.println("<td>");
                out.println("Data Vaccinazione");
                out.println("</td>");



                out.println("<td>");
                out.println("Ora Vaccinazione");
                out.println("</td>");

                out.println("<td>");
                out.println("Medico");
                out.println("</td>");



                out.println("</tr>");





                boolean c = false;
                while (rs.next()) {

                    if (c) {
                        out.println("<tr bgcolor=\"#E6E0E0\">");
                        c = false;
                    } else {
                        out.println("<tr bgcolor=\"white\">");

                        c = true;
                    }


                    out.println("<td>");
                    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    out.println(dateFormat.format(rs.getDate(1)));
                    out.println("</td>");

                    out.println("<td>");
                    dateFormat = new SimpleDateFormat("HH:mm:ss");
                    out.println(dateFormat.format(rs.getTime(1)));
                    out.println("</td>");

                    out.println("<td>");
                    out.println(rs.getString(2) + " " + rs.getString(3));
                    out.println("</td>");





                }
                out.print("                <tr>                 ");
                out.print("                <form action=\"logoff\" method=\"post\"> ");
                out.print("                    <td> ");
                out.print("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
                out.print("                    </td>                   ");
                out.print("                </form> ");
                out.print("                    <td> ");
                out.println("                <form action=\"welcome\" method=\"post\"> ");
                out.println("                      <INPUT TYPE=\"button\" VALUE=\"Tornare\" onClick=\"history.go(-1);\">                         ");

                out.println("                </form> ");
                out.print("                    </td> ");

                out.print("                    <td> ");
                out.print("                    </td> ");

                out.print("                </tr> ");

                out.println("</table>");



                out.println("</body>  ");
                out.println("</html> ");




            } finally {
                out.close();
            }
        } catch (Exception e) {
        }
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
